rsubmit;
data compann; set comp.funda (keep = gvkey cusip datadate sich
ppegt capx csho dltt invt txdb pstkrv ib xint prcc_f datafmt indfmt popsrc consol);
where indfmt='INDL' and datafmt='STD' and popsrc='D' and consol='C';
if (4900<=sich<5000 or 6000<=sich<7000) then delete;
year=year(datadate); month=month(datadate); cusip=substr(cusip,1,8);
if month>6 then year=year+1; if year<1961 or year>2017 then delete;
mve=csho*prcc_f; if nmiss(txdb) then txdb=0; if nmiss(xint) then xint=0;
cusip=substr(cusip,1,8); tq=(csho*prcc_f+dltt+pstkrv-txdb-invt)/ppegt;
pe=(csho*prcc_f+dltt+pstkrv-txdb)/(ib+xint); if tq<=0 then delete; 
keep gvkey cusip year tq pe ppegt capx; proc sort data=compann nodupkey; by gvkey year;
data temp.compannk; set compann; lgvkey=lag(gvkey); lppe=lag(ppegt);
ik=capx/lppe; if gvkey ne lgvkey then ik=.; if ik<=0 then delete;
drop lgvkey lppe ppegt capx; proc means n mean p10 p25 p50 p75 p90; run;

rsubmit;
data delist; set crsp.mse (keep = permno date event dlret dlstcd);
where event="DELIST" and dlret<10 and 1957<year(date)<2018;
year=year(date); month=month(date); drop date; proc sort; by permno year month;
data crsp; set crsp.msf (keep = permno cusip shrout hsiccd date ret prc vol);
where 1957<year(date)<2018; year=year(date); month=month(date); tvol=abs(prc)*vol;
quar=qtr(date); price=abs(prc); me=abs(prc)*shrout;
proc sort; by permno year month; data crsp; merge crsp delist;
by permno year month; drop date prc vol; proc means; run;

data exch; set crsp.mse (keep = date permno exchcd shrcd);
year=year(date); month=month(date); drop date;
proc sort data=exch; by permno year month;
data crsp; merge crsp exch; by permno year month;
data temp.crsp; set crsp; by permno year month;
i=0; do while(i<2); lexchcd = lag(exchcd); lshrcd=lag(shrcd);
if permno = lag(permno) and exchcd = . then exchcd = lexchcd;
if permno = lag(permno) and shrcd = . then shrcd = lshrcd;
i+1; end; if last.month then output; drop i lexchcd lshrcd;
proc sort data=temp.crsp nodupkey; by permno year month;
data temp.crsp; set temp.crsp;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and (exchcd ne 0 
and exchcd ne 3 and exchcd ne 33) then dlret=-0.3;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and
(exchcd=0 or exchcd=3 or exchcd=33) then dlret=-0.55;
if nmiss(ret)=1 and nmiss(dlret)=0 then return=dlret*100;
else return=ret*100; if shrcd ne 10 and shrcd ne 11 then delete;
if (exchcd=3 or exchcd=33) and year<2001 then tvol=tvol/2;
if (exchcd=3 or exchcd=33) and (year=2001 and month=1) then tvol=tvol/2;
if (exchcd=3 or exchcd=33) and (year=2001 and month>1) then tvol=tvol/1.8;
if (exchcd=3 or exchcd=33) and (year=2002 or year=2003) then tvol=tvol/1.6;
turn=tvol/me/10; drop event ret dlret dlstcd shrcd hsiccd; proc means; run;

rsubmit;
data gvkey; set crsp.ccmxpf_linktable (keep = gvkey lpermno linkdt linkenddt);
where nmiss(lpermno)=0; linkstart=(year(linkdt)-1963)*12+month(linkdt);
linkend=(year(linkenddt)-1963)*12+month(linkenddt); if nmiss(linkend) then linkend=684;
data cap; set crsp.msf (keep = permno cusip date); where 1961<year(date)<2020;
year=year(date); month=month(date); yearm=(year-1963)*12+month; drop date; proc sort; by permno yearm;
proc sql; create table snpgvkey as select gvkey.linkstart, gvkey.linkend, gvkey.gvkey, cap.*
from cap, gvkey where gvkey.linkstart<=cap.yearm<gvkey.linkend and cap.permno=gvkey.lpermno; quit;
proc sort nodupkey; by gvkey year month; proc means; proc print data=snpgvkey (obs=100); run;

rsubmit;
options source notes errors=5;
data compann; set temp.compannk; year=year+1; proc sort; by gvkey year;
data compann; merge compann snpgvkey; by gvkey year; proc sort; by permno year month;
data ivol; merge temp.ivol18 temp.betacapm18 compann; by permno year month;
data return; set temp.crsp (keep = permno year month return);
data ivol; merge ivol return; by permno year month;
data cap; set temp.crsp (keep = permno year month price exchcd me);
where month=12; year=year+1; proc sort; by permno year;
data ivol; merge cap ivol; by permno year; proc sort; by year month; run;

proc univariate data=ivol noprint;
var ivol; by year month; where exchcd=1;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  pdecile=1;
  IF ivol > DEC10 THEN pdecile=2;
  IF ivol > DEC20 THEN pdecile=3;
  IF ivol > DEC30 THEN pdecile=4;
  IF ivol > DEC40 THEN pdecile=5;
  IF ivol > DEC50 THEN pdecile=6;
  IF ivol > DEC60 THEN pdecile=7;
  IF ivol > DEC70 THEN pdecile=8;
  IF ivol > DEC80 THEN pdecile=9;
  IF ivol > DEC90 THEN pdecile=10;
  if nmiss(ivol) then pdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var beta; by year month; where exchcd=1;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  bdecile=1;
  IF beta > DEC10 THEN bdecile=2;
  IF beta > DEC20 THEN bdecile=3;
  IF beta > DEC30 THEN bdecile=4;
  IF beta > DEC40 THEN bdecile=5;
  IF beta > DEC50 THEN bdecile=6;
  IF beta > DEC60 THEN bdecile=7;
  IF beta > DEC70 THEN bdecile=8;
  IF beta > DEC80 THEN bdecile=9;
  IF beta > DEC90 THEN bdecile=10;
  if nmiss(beta) then bdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var pe; by year; where exchcd=1 and pe>0;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year;
  IF pe < 0 THEN pedecile=0;
  IF 0 <= pe < DEC10 THEN pedecile=1;
  IF pe > DEC10 THEN pedecile=2;
  IF pe > DEC20 THEN pedecile=3;
  IF pe > DEC30 THEN pedecile=4;
  IF pe > DEC40 THEN pedecile=5;
  IF pe > DEC50 THEN pedecile=6;
  IF pe > DEC60 THEN pedecile=7;
  IF pe > DEC70 THEN pedecile=8;
  IF pe > DEC80 THEN pedecile=9;
  IF pe > DEC90 THEN pedecile=10;
  if nmiss(pe) then pedecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var ik; by year; where exchcd=1;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year;
  ikdecile=1;
  IF ik > DEC10 THEN ikdecile=2;
  IF ik > DEC20 THEN ikdecile=3;
  IF ik > DEC30 THEN ikdecile=4;
  IF ik > DEC40 THEN ikdecile=5;
  IF ik > DEC50 THEN ikdecile=6;
  IF ik > DEC60 THEN ikdecile=7;
  IF ik > DEC70 THEN ikdecile=8;
  IF ik > DEC80 THEN ikdecile=9;
  IF ik > DEC90 THEN ikdecile=10;
  if nmiss(ik) then ikdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var tq; by year; where exchcd=1;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year;
  tqdecile=1;
  IF tq > DEC10 THEN tqdecile=2;
  IF tq > DEC20 THEN tqdecile=3;
  IF tq > DEC30 THEN tqdecile=4;
  IF tq > DEC40 THEN tqdecile=5;
  IF tq > DEC50 THEN tqdecile=6;
  IF tq > DEC60 THEN tqdecile=7;
  IF tq > DEC70 THEN tqdecile=8;
  IF tq > DEC80 THEN tqdecile=9;
  IF tq > DEC90 THEN tqdecile=10;
  if nmiss(tq) then tqdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc sort data=ivol; by cusip year month;
data ivol; set ivol; lcusip=lag(cusip);
ivoldecile=lag(pdecile); betadecile=lag(bdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then betadecile=.;
proc means nolabels; run;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month ivoldecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where ivoldecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleivol10vw"; RUN;
rsubmit;
proc sort data=ivol; by year month ivoldecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where ivoldecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleivol10ew"; RUN;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month betadecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where betadecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlebetavw"; RUN;
rsubmit;
proc sort data=ivol; by year month betadecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where betadecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlebetaew"; RUN;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month tqdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where tqdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singletqvw"; RUN;
rsubmit;
proc sort data=ivol; by year month tqdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where tqdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singletqew"; RUN;

rsubmit;
proc sort data=ivol; by year month pedecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where pedecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlepevw"; RUN;
rsubmit;
proc sort data=ivol; by year month pedecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where pedecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlepeew"; RUN;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month ikdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where ikdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleikvw"; RUN;
rsubmit;
proc sort data=ivol; by year month ikdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where ikdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleikew"; RUN;

rsubmit;
data gvkey; set crsp.ccmxpf_linktable (keep = gvkey lpermno linkdt linkenddt);
where nmiss(lpermno)=0; linkstart=(year(linkdt)-1963)*12+month(linkdt);
linkend=(year(linkenddt)-1963)*12+month(linkenddt); if nmiss(linkend) then linkend=684;
data cap; set crsp.msf (keep = permno cusip date); where 1961<year(date)<2020;
year=year(date); month=month(date); yearm=(year-1963)*12+month; drop date; proc sort; by permno yearm;
proc sql; create table snpgvkey as select gvkey.linkstart, gvkey.linkend, gvkey.gvkey, cap.*
from cap, gvkey where gvkey.linkstart<=cap.yearm<gvkey.linkend and cap.permno=gvkey.lpermno; quit;
proc sort nodupkey; by gvkey year month; proc means; proc print data=snpgvkey (obs=100); run;

rsubmit;
options source notes errors=5;
data compann; set temp.compannk; year=year+1; proc sort; by gvkey year;
data compann; merge compann snpgvkey; by gvkey year; proc sort; by permno year month;
data ivol; merge temp.ivol18 temp.betacapm18 compann; by permno year month;
data return; set temp.crsp (keep = permno year month return);
data ivol; merge ivol return; by permno year month;
data cap; set temp.crsp (keep = permno year month price exchcd me);
where month=12; year=year+1; proc sort; by permno year;
data ivol; merge cap ivol; by permno year; proc sort; by year month; run;

proc univariate data=ivol noprint;
var ivol; by year month; where exchcd=1 and price ge 5;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  pdecile=1;
  IF ivol > DEC10 THEN pdecile=2;
  IF ivol > DEC20 THEN pdecile=3;
  IF ivol > DEC30 THEN pdecile=4;
  IF ivol > DEC40 THEN pdecile=5;
  IF ivol > DEC50 THEN pdecile=6;
  IF ivol > DEC60 THEN pdecile=7;
  IF ivol > DEC70 THEN pdecile=8;
  IF ivol > DEC80 THEN pdecile=9;
  IF ivol > DEC90 THEN pdecile=10;
  if nmiss(ivol) or price<5 then pdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var beta; by year month; where exchcd=1 and price ge 5;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  bdecile=1;
  IF beta > DEC10 THEN bdecile=2;
  IF beta > DEC20 THEN bdecile=3;
  IF beta > DEC30 THEN bdecile=4;
  IF beta > DEC40 THEN bdecile=5;
  IF beta > DEC50 THEN bdecile=6;
  IF beta > DEC60 THEN bdecile=7;
  IF beta > DEC70 THEN bdecile=8;
  IF beta > DEC80 THEN bdecile=9;
  IF beta > DEC90 THEN bdecile=10;
  if nmiss(beta) or price<5 then bdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var pe; by year; where exchcd=1 and price ge 5 and pe>0;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year;
  IF pe < 0 THEN pedecile=0;
  IF 0 <= pe < DEC10 THEN pedecile=1;
  IF pe > DEC10 THEN pedecile=2;
  IF pe > DEC20 THEN pedecile=3;
  IF pe > DEC30 THEN pedecile=4;
  IF pe > DEC40 THEN pedecile=5;
  IF pe > DEC50 THEN pedecile=6;
  IF pe > DEC60 THEN pedecile=7;
  IF pe > DEC70 THEN pedecile=8;
  IF pe > DEC80 THEN pedecile=9;
  IF pe > DEC90 THEN pedecile=10;
  if nmiss(pe) or price<5 then pedecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var ik; by year; where exchcd=1 and price ge 5;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year;
  ikdecile=1;
  IF ik > DEC10 THEN ikdecile=2;
  IF ik > DEC20 THEN ikdecile=3;
  IF ik > DEC30 THEN ikdecile=4;
  IF ik > DEC40 THEN ikdecile=5;
  IF ik > DEC50 THEN ikdecile=6;
  IF ik > DEC60 THEN ikdecile=7;
  IF ik > DEC70 THEN ikdecile=8;
  IF ik > DEC80 THEN ikdecile=9;
  IF ik > DEC90 THEN ikdecile=10;
  if nmiss(ik) or price<5 then ikdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc univariate data=ivol noprint;
var tq; by year; where exchcd=1 and price ge 5;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year;
  tqdecile=1;
  IF tq > DEC10 THEN tqdecile=2;
  IF tq > DEC20 THEN tqdecile=3;
  IF tq > DEC30 THEN tqdecile=4;
  IF tq > DEC40 THEN tqdecile=5;
  IF tq > DEC50 THEN tqdecile=6;
  IF tq > DEC60 THEN tqdecile=7;
  IF tq > DEC70 THEN tqdecile=8;
  IF tq > DEC80 THEN tqdecile=9;
  IF tq > DEC90 THEN tqdecile=10;
  if nmiss(tq) or price<5 then tqdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;

proc sort data=ivol; by cusip year month;
data ivol; set ivol; lcusip=lag(cusip);
ivoldecile=lag(pdecile); betadecile=lag(bdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then betadecile=.;
proc means nolabels; run;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month ivoldecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where ivoldecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleivol10vw5"; RUN;
rsubmit;
proc sort data=ivol; by year month ivoldecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where ivoldecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleivol10ew5"; RUN;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month betadecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where betadecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlebetavw5"; RUN;
rsubmit;
proc sort data=ivol; by year month betadecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where betadecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlebetaew5"; RUN;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month tqdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where tqdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singletqvw5"; RUN;
rsubmit;
proc sort data=ivol; by year month tqdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where tqdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singletqew5"; RUN;

rsubmit;
proc sort data=ivol; by year month pedecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where pedecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlepevw5"; RUN;
rsubmit;
proc sort data=ivol; by year month pedecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where pedecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singlepeew5"; RUN;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month ikdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight me;
where ikdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleikvw5"; RUN;
rsubmit;
proc sort data=ivol; by year month ikdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where ikdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="singleikew5"; RUN;
